//
//  GKNovelNetManager.swift
//  MySwiftObject
//
//  Created by wangws1990 on 2019/9/10.
//  Copyright © 2019 wangws1990. All rights reserved.
//

import UIKit

class GKNovelNetManager: NSObject {
    class func bookSummary(bookId:String,sucesss:@escaping ((_ source : GKNovelSource) ->Void),failure:@escaping ((_ error : String) ->Void)){
        BaseNetManager.iGetUrlString(urlString: BaseNetManager.hostUrl(txcode:"toc"), parameters: ["book":bookId,"view":"summary"], sucesss: { (object) in
            if object is NSArray{
                let datas :NSArray = object as! NSArray;
                let dic:NSDictionary = datas.firstObject as! NSDictionary;
                let model :GKNovelSource = GKNovelSource.deserialize(from: dic)!;
                sucesss(model);
            }
        }, failure: failure);
    };
    class func bookChapters(bookId:String,sucesss:@escaping ((_ object : GKNovelChapterInfo) ->Void),failure:@escaping ((_ error : String) ->Void)){
        let url :String = "toc/" + String(bookId);
        BaseNetManager.iGetUrlString(urlString: BaseNetManager.hostUrl(txcode:url), parameters: ["view":"chapters"], sucesss: { (object) in
            if object is NSDictionary{
                let dic :NSDictionary = object as! NSDictionary;
                let info :GKNovelChapterInfo = GKNovelChapterInfo.deserialize(from: dic)!;
                sucesss(info);
            }
        }, failure: failure);
    };
    class func bookContent(link:String,sucesss:@escaping ((_ object : GKNovelContent) ->Void),failure:@escaping ((_ error : String) ->Void)){
        let  string:String = link.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        let url :String = "https://chapter2.zhuishushenqi.com/chapter/" + String(string);
        BaseNetManager.iGetUrlString(urlString:url, parameters: [:], sucesss: { (object) in
            if object is NSDictionary{
                let dic:NSDictionary = object as! NSDictionary;
                let respond:NSDictionary = dic.object(forKey: "chapter") as! NSDictionary
                let model:GKNovelContent = GKNovelContent.deserialize(from: respond)!;
                sucesss(model);
            }
        }, failure: failure)
    };
    class func bookContentModel(bookId:String, model:GKNovelChapter,sucesss:@escaping ((_ object : GKNovelContent) ->Void),failure:@escaping ((_ error : String) ->Void)){
        GKBookCacheDataQueue.getContentFromDataBase(bookId:bookId, chapterId: model.chapterId) { (content) in
            if content.content != nil{
                sucesss(content);
            }else{
                GKNovelNetManager.bookContent(link: model.link, sucesss: { (content) in
                    content.bookId = model.bookId;
                    sucesss(content);
                    GKBookCacheDataQueue.insertContentToDataBase(bookId:bookId, content: content, completion: { (success) in
                        
                    })
                }, failure:failure);
            }
        };
    }
}
